//
//  ZZHomeViewController.swift
//  SinaWeibo
//
//  Created by QUAN on 15/11/9.
//  Copyright © 2015年 QUAN. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD

//cellID
private let HomeCellId = "HomeCellId"

class ZZHomeViewController: ZZBaseViewController {
    
    
    //数据源  模型数组
    private lazy var statuses = [ZZStatus]()
    
    override func viewDidLoad() {
        
        //判断有没有登录
        if !userLogin {
            visitorLoginView?.setUIInfo(nil, title: "关注一些人，回这里看看有什么惊喜")
            return
        }
        
        loadData()
        
        prepareTableView()
    }
    
    @objc private func loadData() {
        ZZStatusListViewModel.loadData { (array) -> () in
         
            self.refreshControl?.endRefreshing()
            if array == nil {
                SVProgressHUD.showErrorWithStatus(AppErrorTip)
                return
            }
            self.statuses = array!
            
            self.tableView.reloadData()
        }
        
    }
    
    
    private func prepareTableView() {
        
        //添加下拉事件
        refreshControl = UIRefreshControl()
        
        refreshControl?.addTarget(self, action: "loadData", forControlEvents: .ValueChanged)
        
        tableView.registerClass(ZZStatusCell.self, forCellReuseIdentifier: HomeCellId)
        
        //预估行高
        tableView.estimatedRowHeight = 200
        //自动计算行高
        tableView.rowHeight = UITableViewAutomaticDimension
        
    }
    
}

//数据源方法, 代理方法
extension ZZHomeViewController {
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statuses.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //创建cell
        let cell = tableView.dequeueReusableCellWithIdentifier(HomeCellId, forIndexPath: indexPath) as! ZZStatusCell
        
//        cell.textLabel?.text = statuses[indexPath.row].user?.name
        
        cell.status = statuses[indexPath.row]
        
        return cell
    }
    
}

